package com.hejia.bulbul.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import com.hejia.alauda.entity.Popularize;
import com.hejia.alauda.entity.PopularizeCategory;
import com.hejia.alauda.entity.PopularizeCount;
import com.hejia.alauda.entity.example.PopularizeCategoryExample;
import com.hejia.alauda.entity.example.PopularizeExample;
import com.hejia.alauda.persistence.mybatis.PopularizeCategoryMapper;
import com.hejia.alauda.persistence.mybatis.PopularizeMapper;
import com.hejia.alauda.persistence.mybatis.PopularizeVisitMapper;
import com.hejia.alauda.utils.DateTimeUtil;
import com.hejia.alauda.utils.Pager;
import com.hejia.bulbul.entity.PopularizeStatistic;

@Service
public class PopularizeStatisticServiceImpl implements
		PopularizeStatisticService {
	
	@Resource
	private MongoTemplate mongoTemplate;
	
	@Resource
	private PopularizeCategoryMapper popularizeCategoryMapper;
	
	@Resource
	private PopularizeVisitMapper popularizeVisitMapper;
	
	@Resource
	private PopularizeMapper popularizeMapper;
	
	@Override
	public Pager<PopularizeStatistic> statisticPopularizes(
			Date startDate,
			Date endDate, 
			Pager<PopularizeStatistic> pager) {
		
		List<PopularizeStatistic> statistics = findPopularizeStatisticByBiggerCategory(pager).getList();
		if(statistics.size() > 0){
			setVisitIPCount(statistics, startDate, endDate);
			setRegisterCount(statistics, startDate, endDate);
			setRealNameUserCount(statistics, startDate, endDate);
			setPaidUserCount(statistics, startDate, endDate);
			setOrderCount(statistics, startDate, endDate);
			setPaidOrderCount(statistics, startDate, endDate);
			setPaymentAmountCount(statistics, startDate, endDate);
		}
		
		return pager;
	}

	@Override
	public Pager<PopularizeStatistic> findPopularizeStatisticByBiggerCategory(Pager<PopularizeStatistic> pager) {
		
		Pager<PopularizeCategory> categoryPager = new Pager<PopularizeCategory>();
		categoryPager.setPageSize(pager.getPageSize());
		categoryPager.setPageNumber(pager.getPageNumber());
		
		PopularizeCategoryExample example = new PopularizeCategoryExample();
		example.appendCriterion("bigger_id is null and isStopPopularize != 1");
		example.setOrderByClause("createDate desc");
		
		List<PopularizeStatistic> statistics = new ArrayList<PopularizeStatistic>();
		List<PopularizeCategory> categorys = this.popularizeCategoryMapper.selectByExample(example, categoryPager);
		for(PopularizeCategory category : categorys){
			PopularizeStatistic statistic = new PopularizeStatistic();
			statistic.setBiggerCategoryId(category.getId());
			statistic.setBiggerCategoryName(category.getName());
			statistics.add(statistic);
		}
		
		pager.setList(statistics);
		pager.setTotalCount(this.popularizeCategoryMapper.countByExample(example));
		return pager;
	}

	/**
	 * 设置访问IP数
	 * @param statistics
	 * @param startDate
	 * @param endDate
	 */
	@Override
	public void setVisitIPCount(List<PopularizeStatistic> statistics, Date startDate, Date endDate) {
		
		List<Long> biggerCategoryIds = getBiggerCategoryIds(statistics);
		List<PopularizeCount> list = this.popularizeVisitMapper
				.countVisitIPByBiggerCategorysAndDate(biggerCategoryIds, startDate, endDate);
		
		for (PopularizeCount popularizeCount : list) {
			for (PopularizeStatistic statistic : statistics) {
				if (popularizeCount.getBiggerCategoryId().equals(statistic.getBiggerCategoryId())) {
					statistic.setVisitCount(popularizeCount.getCount());
				}
			}
		}
		
	}

	/**
	 * 设置下单支付金额
	 * @param statistics	统计列表
	 * @param startDate		开始时间
	 * @param endDate		结束时间
	 */
	private void setPaymentAmountCount(List<PopularizeStatistic> statistics,
			Date startDate, Date endDate) {
		
		List<Long> biggerCategoryIds = getBiggerCategoryIds(statistics);
		List<PopularizeCount> list = this.popularizeVisitMapper
				.countPaymentAmountBiggerCategorysAndDate(biggerCategoryIds, startDate, endDate);
		
		for (PopularizeCount popularizeCount : list) {
			for (PopularizeStatistic statistic : statistics) {
				if (popularizeCount.getBiggerCategoryId().equals(statistic.getBiggerCategoryId())) {
					statistic.setPaymentAmount(popularizeCount.getAmount());
				}
			}
		}
	}

	/**
	 * 设置支付订单数
	 * @param statistics	统计列表
	 * @param startDate		开始时间
	 * @param endDate		结束时间
	 */
	private void setPaidOrderCount(List<PopularizeStatistic> statistics,
			Date startDate, Date endDate) {
		
		List<Long> biggerCategoryIds = getBiggerCategoryIds(statistics);
		List<PopularizeCount> list = this.popularizeVisitMapper
				.countPaidOrderByBiggerCategorysAndDate(biggerCategoryIds, startDate, endDate);
		
		for (PopularizeCount popularizeCount : list) {
			for (PopularizeStatistic statistic : statistics) {
				if (popularizeCount.getBiggerCategoryId().equals(statistic.getBiggerCategoryId())) {
					statistic.setPaidOrderCount(popularizeCount.getCount());
				}
			}
		}
	}

	/**
	 * 设置下单数
	 * @param statistics	统计列表
	 * @param startDate		开始时间
	 * @param endDate		结束时间
	 */
	private void setOrderCount(List<PopularizeStatistic> statistics,
			Date startDate, Date endDate) {
		
		List<Long> biggerCategoryIds = getBiggerCategoryIds(statistics);
		List<PopularizeCount> list = this.popularizeVisitMapper
				.countOrderByBiggerCategorysAndDate(biggerCategoryIds, startDate, endDate);
		
		for (PopularizeCount popularizeCount : list) {
			for (PopularizeStatistic statistic : statistics) {
				if (popularizeCount.getBiggerCategoryId().equals(statistic.getBiggerCategoryId())) {
					statistic.setOrderCount(popularizeCount.getCount());
				}
			}
		}
	}

	/**
	 * 设置下单支付用户数
	 * @param statistics	统计列表
	 * @param startDate		开始时间
	 * @param endDate		结束时间
	 */
	private void setPaidUserCount(List<PopularizeStatistic> statistics,
			Date startDate, Date endDate) {
		
		List<Long> biggerCategoryIds = getBiggerCategoryIds(statistics);
		List<PopularizeCount> list = this.popularizeVisitMapper
				.countPaidUserByBiggerCategorysAndDate(biggerCategoryIds, startDate, endDate);
		
		for (PopularizeCount popularizeCount : list) {
			for (PopularizeStatistic statistic : statistics) {
				if (popularizeCount.getBiggerCategoryId().equals(statistic.getBiggerCategoryId())) {
					statistic.setPaidUserCount(popularizeCount.getCount());
				}
			}
		}
		
	}

	/**
	 * 设置实名认证用户数
	 * @param statistics	统计列表
	 * @param startDate		开始时间
	 * @param endDate		结束时间
	 */
	private void setRealNameUserCount(List<PopularizeStatistic> statistics,
			Date startDate, Date endDate) {
		
		List<Long> biggerCategoryIds = getBiggerCategoryIds(statistics);
		List<PopularizeCount> list = this.popularizeVisitMapper
				.countRealNameUserByBiggerCategorysAndDate(biggerCategoryIds, startDate, endDate);
		
		for (PopularizeCount popularizeCount : list) {
			for (PopularizeStatistic statistic : statistics) {
				if (popularizeCount.getBiggerCategoryId().equals(statistic.getBiggerCategoryId())) {
					statistic.setRealNameCount(popularizeCount.getCount());
				}
			}
		}
	}

	/**
	 * 设置注册数
	 * @param statistics	统计列表
	 * @param startDate		开始时间
	 * @param endDate		结束时间
	 */
	private void setRegisterCount(List<PopularizeStatistic> statistics,
			Date startDate, Date endDate) {
		
		List<Long> biggerCategoryIds = getBiggerCategoryIds(statistics);
		List<PopularizeCount> list = this.popularizeVisitMapper
				.countUserRegisterByBiggerCategorysAndDate(biggerCategoryIds, startDate, endDate);
		
		for (PopularizeCount popularizeCount : list) {
			for (PopularizeStatistic statistic : statistics) {
				if (popularizeCount.getBiggerCategoryId().equals(statistic.getBiggerCategoryId())) {
					statistic.setRegisterCount(popularizeCount.getCount());
				}
			}
		}
	}
	
	/**
	 * 获取稍大类别ID列表
	 * @param statistics
	 * @return
	 */
	private List<Long> getBiggerCategoryIds(List<PopularizeStatistic> statistics) {
		List<Long> biggerCategoryIds = new ArrayList<>();
		for (PopularizeStatistic statistic : statistics) {
			biggerCategoryIds.add(statistic.getBiggerCategoryId());
			
		}
		return biggerCategoryIds;
	}

	@Override
	public List<PopularizeStatistic> statisticPopularizeByBiggerCategoryIdAndDate(
			Long biggerCategoryId, Date startDate, Date endDate) {
		
		startDate = DateTimeUtil.getDateDay(startDate);
		endDate = DateTimeUtil.getDateAndMaxTime(endDate);

		List<PopularizeStatistic> statistics = getPopularizeStatisticByBiggerCategoryId(biggerCategoryId);
		
		if(statistics.size() > 0){
			countVisitIPByBiggerCategoryIdAndDate(biggerCategoryId, startDate, endDate, statistics);
			countRegisterByBiggerCategoryIdAndDate(biggerCategoryId, startDate, endDate, statistics);
			countRealNameUserByBiggerCategoryIdAndDate(biggerCategoryId, startDate, endDate, statistics);
			countPaidUserByBiggerCategoryIdAndDate(biggerCategoryId, startDate, endDate, statistics);
			countOrderByBiggerCategoryIdAndDate(biggerCategoryId, startDate, endDate, statistics);
			countPaidOrderByBiggerCategoryIdAndDate(biggerCategoryId, startDate, endDate, statistics);
			countPaymentAmountByBiggerCategoryIdAndDate(biggerCategoryId, startDate, endDate, statistics);
		}
		
		return getNotStopPopularizeStatistics(biggerCategoryId, statistics);
	}

	/**
	 * 统计访问IP数
	 * @param biggerCategoryId
	 * @param startDate
	 * @param endDate
	 * @param statistics
	 */
	@Override
	public void countVisitIPByBiggerCategoryIdAndDate(Long biggerCategoryId, Date startDate, Date endDate, List<PopularizeStatistic> statistics) {
		
		List<PopularizeCount> counts = this.popularizeVisitMapper
				.countVisitIPByBiggerCategoryIdAndDate(biggerCategoryId, startDate, endDate);
		
		for(PopularizeCount count : counts){
			for(PopularizeStatistic statistic : statistics){
				if(statistic.getCategoryId().equals(count.getCategoryId())){
					statistic.setVisitCount(count.getCount());
				}
			}
		}
		
	}

	/**
	 * 获取未停止推广统计
	 * @param biggerCategoryId	大类ID
	 * @param statistics		统计列表
	 * @return					未停止推广统计
	 */
	@Override
	public List<PopularizeStatistic> getNotStopPopularizeStatistics(
			Long biggerCategoryId, List<PopularizeStatistic> statistics) {
		
		PopularizeStatistic stopPopularizeStatistic = new PopularizeStatistic();
		stopPopularizeStatistic.setBiggerCategoryId(biggerCategoryId);
		stopPopularizeStatistic.setCategoryId(Long.MAX_VALUE);
		stopPopularizeStatistic.setCategoryName("停止推广");
		
		List<PopularizeStatistic> notStopPopularizeStatistics = new ArrayList<>();
		for(PopularizeStatistic statistic : statistics){
			
			// 统计全为零，不添加
			if(this.isPopularizeStatisticAllZero(statistic)){
				continue;
			}
						
			if(statistic.getIsStopPopularize()){
				stopPopularizeStatistic.setVisitCount(stopPopularizeStatistic.getVisitCount() + statistic.getVisitCount());
				stopPopularizeStatistic.setRegisterCount(stopPopularizeStatistic.getRegisterCount() + statistic.getRegisterCount());
				stopPopularizeStatistic.setRealNameCount(stopPopularizeStatistic.getRealNameCount() + statistic.getRealNameCount());
				stopPopularizeStatistic.setPaidUserCount(stopPopularizeStatistic.getPaidUserCount() + statistic.getPaidUserCount());
				stopPopularizeStatistic.setOrderCount(stopPopularizeStatistic.getOrderCount() + statistic.getOrderCount());
				stopPopularizeStatistic.setPaidOrderCount(stopPopularizeStatistic.getPaidOrderCount() + statistic.getPaidOrderCount());
				stopPopularizeStatistic.setPaymentAmount(stopPopularizeStatistic.getPaymentAmount().add(statistic.getPaymentAmount()));
			}else{
				notStopPopularizeStatistics.add(statistic);
			}
		}
		
		if(!isPopularizeStatisticAllZero(stopPopularizeStatistic)){
			notStopPopularizeStatistics.add(stopPopularizeStatistic);
		}
		
		return notStopPopularizeStatistics;
	}

	/**
	 * 根据大类ID获取统计列表
	 * @param biggerCategoryId	大类ID
	 * @return
	 */
	@Override
	public List<PopularizeStatistic> getPopularizeStatisticByBiggerCategoryId(
			Long biggerCategoryId) {
		
		PopularizeCategoryExample example = new PopularizeCategoryExample();
		example.appendCriterion("bigger_id =", biggerCategoryId);
		example.setOrderByClause("createDate desc");
		List<PopularizeCategory> categories = this.popularizeCategoryMapper.selectByExample(example, null);
		
		List<PopularizeStatistic> statistics = new ArrayList<>();
		for(PopularizeCategory category : categories){
			PopularizeStatistic statistic = new PopularizeStatistic();
			statistic.setBiggerCategoryId(biggerCategoryId);
			statistic.setCategoryId(category.getId());
			statistic.setCategoryName(category.getName());
			statistic.setIsStopPopularize(category.getIsStopPopularize());
			statistics.add(statistic);
		}
		return statistics;
	}

	/**
	 * 统计支付订单数
	 * @param biggerCategoryId	大类ID
	 * @param startDate			开始日期
	 * @param endDate			结束日期
	 * @param statistics		统计
	 */
	private void countPaymentAmountByBiggerCategoryIdAndDate(Long biggerCategoryId,
			Date startDate, Date endDate, List<PopularizeStatistic> statistics) {
		
		List<PopularizeCount> counts = this.popularizeVisitMapper
				.countPaymentAmountByBiggerCategoryIdAndDate(biggerCategoryId, startDate, endDate);
		
		for(PopularizeCount count : counts){
			for(PopularizeStatistic statistic : statistics){
				if(statistic.getCategoryId().equals(count.getCategoryId())){
					statistic.setPaymentAmount(count.getAmount());
				}
			}
		}
	}
	
	/**
	 * 统计支付订单数
	 * @param biggerCategoryId	大类ID
	 * @param startDate			开始日期
	 * @param endDate			结束日期
	 * @param statistics		统计
	 */
	private void countPaidOrderByBiggerCategoryIdAndDate(Long biggerCategoryId,
			Date startDate, Date endDate, List<PopularizeStatistic> statistics) {
		
		List<PopularizeCount> counts = this.popularizeVisitMapper
				.countPaidOrderByBiggerCategoryIdAndDate(biggerCategoryId, startDate, endDate);
		
		for(PopularizeCount count : counts){
			for(PopularizeStatistic statistic : statistics){
				if(statistic.getCategoryId().equals(count.getCategoryId())){
					statistic.setPaidOrderCount(count.getCount());
				}
			}
		}
	}
	
	/**
	 * 统计下单数
	 * @param biggerCategoryId	大类ID
	 * @param startDate			开始日期
	 * @param endDate			结束日期
	 * @param statistics		统计
	 */
	private void countOrderByBiggerCategoryIdAndDate(Long biggerCategoryId,
			Date startDate, Date endDate, List<PopularizeStatistic> statistics) {
		
		List<PopularizeCount> counts = this.popularizeVisitMapper
				.countOrderByBiggerCategoryIdAndDate(biggerCategoryId, startDate, endDate);
		
		for(PopularizeCount count : counts){
			for(PopularizeStatistic statistic : statistics){
				if(statistic.getCategoryId().equals(count.getCategoryId())){
					statistic.setOrderCount(count.getCount());
				}
			}
		}
	}
	
	/**
	 * 统计下单支付用户数
	 * @param biggerCategoryId	大类ID
	 * @param startDate			开始日期
	 * @param endDate			结束日期
	 * @param statistics		统计
	 */
	private void countPaidUserByBiggerCategoryIdAndDate(Long biggerCategoryId,
			Date startDate, Date endDate, List<PopularizeStatistic> statistics) {
		
		List<PopularizeCount> counts = this.popularizeVisitMapper
				.countPaidUserByBiggerCategoryIdAndDate(biggerCategoryId, startDate, endDate);
		
		for(PopularizeCount count : counts){
			for(PopularizeStatistic statistic : statistics){
				if(statistic.getCategoryId().equals(count.getCategoryId())){
					statistic.setPaidUserCount(count.getCount());
				}
			}
		}
	}
	
	/**
	 * 统计实名认证用户数
	 * @param biggerCategoryId	大类ID
	 * @param startDate			开始日期
	 * @param endDate			结束日期
	 * @param statistics		统计
	 */
	private void countRealNameUserByBiggerCategoryIdAndDate(Long biggerCategoryId,
			Date startDate, Date endDate, List<PopularizeStatistic> statistics) {
		
		List<PopularizeCount> counts = this.popularizeVisitMapper
				.countRealNameUserByBiggerCategoryIdAndDate(biggerCategoryId, startDate, endDate);
		
		for(PopularizeCount count : counts){
			for(PopularizeStatistic statistic : statistics){
				if(statistic.getCategoryId().equals(count.getCategoryId())){
					statistic.setRealNameCount(count.getCount());
				}
			}
		}
	}
	
	/**
	 * 统计注册用户数
	 * @param biggerCategoryId	大类ID
	 * @param startDate			开始日期
	 * @param endDate			结束日期
	 * @param statistics		统计
	 */
	private void countRegisterByBiggerCategoryIdAndDate(Long biggerCategoryId,
			Date startDate, Date endDate, List<PopularizeStatistic> statistics) {
		
		List<PopularizeCount> counts = this.popularizeVisitMapper
				.countUserRegisterByBiggerCategoryIdAndDate(biggerCategoryId, startDate, endDate);
		
		for(PopularizeCount count : counts){
			for(PopularizeStatistic statistic : statistics){
				if(statistic.getCategoryId().equals(count.getCategoryId())){
					statistic.setRegisterCount(count.getCount());
				}
			}
		}
	}

	@Override
	public List<PopularizeStatistic> statisticPopularizeByCategoryIdAndDate(
			Long biggerCategoryId, Long categoryId, Date startDate, Date endDate) {

		startDate = DateTimeUtil.getDateDay(startDate);
		endDate = DateTimeUtil.getDateAndMaxTime(endDate);
		
		List<PopularizeStatistic> statistics = getPopularizeStatisticByCategoryId(
				biggerCategoryId, categoryId);
		
		if(statistics.size() > 0){
			countVisitIPByCategoryIdAndDate(biggerCategoryId, categoryId, startDate, endDate, statistics);
			countRegisterByCategoryIdAndDate(biggerCategoryId, categoryId, startDate, endDate, statistics);
			countRealNameUserByCategoryIdAndDate(biggerCategoryId, categoryId, startDate, endDate, statistics);
			countPaidUserByCategoryIdAndDate(biggerCategoryId, categoryId, startDate, endDate, statistics);
			countOrderByCategoryIdAndDate(biggerCategoryId, categoryId, startDate, endDate, statistics);
			countPaidOrderByCategoryIdAndDate(biggerCategoryId, categoryId, startDate, endDate, statistics);
			countPaymentAmountByCategoryIdAndDate(biggerCategoryId, categoryId, startDate, endDate, statistics);
		}
		
		return getNotStopPopularizeStatistics(biggerCategoryId, categoryId, statistics);
	}

	/**
	 * 统计访问IP数
	 * @param biggerCategoryId
	 * @param categoryId
	 * @param startDate
	 * @param endDate
	 * @param statistics
	 */
	@Override
	public void countVisitIPByCategoryIdAndDate(Long biggerCategoryId, Long categoryId, Date startDate, Date endDate, List<PopularizeStatistic> statistics) {
		
		List<PopularizeCount> counts = this.popularizeVisitMapper
				.countVisitIPByCategoryIdAndDate(categoryId, startDate, endDate);
		for(PopularizeCount count : counts){
			for(PopularizeStatistic statistic : statistics){
				if(statistic.getPopularizeId().equals(count.getPopularizeId())){
					statistic.setVisitCount(count.getCount());
				}
			}
		}
		
	}

	/**
	 * 获取未停止推广统计
	 * @param statistics	推广统计
	 * @return				未停止推广统计
	 */
	@Override
	public List<PopularizeStatistic> getNotStopPopularizeStatistics(
			Long biggerCategoryId, 
			Long categoryId,
			List<PopularizeStatistic> statistics) {
		
		PopularizeStatistic stopPopularizeStatistic = new PopularizeStatistic();
		stopPopularizeStatistic.setBiggerCategoryId(biggerCategoryId);
		stopPopularizeStatistic.setCategoryId(categoryId);
		stopPopularizeStatistic.setPopularizeId(Long.MAX_VALUE);
		stopPopularizeStatistic.setPopularizeName("停止推广");
		
		List<PopularizeStatistic> notStopPopularizeStatistics = new ArrayList<>();
		for(PopularizeStatistic statistic : statistics){
			
			// 统计全为零，不添加
			if(this.isPopularizeStatisticAllZero(statistic)){
				continue;
			}
			
			if(statistic.getIsStopPopularize()){
				stopPopularizeStatistic.setVisitCount(stopPopularizeStatistic.getVisitCount() + statistic.getVisitCount());
				stopPopularizeStatistic.setRegisterCount(stopPopularizeStatistic.getRegisterCount() + statistic.getRegisterCount());
				stopPopularizeStatistic.setRealNameCount(stopPopularizeStatistic.getRealNameCount() + statistic.getRealNameCount());
				stopPopularizeStatistic.setPaidUserCount(stopPopularizeStatistic.getPaidUserCount() + statistic.getPaidUserCount());
				stopPopularizeStatistic.setOrderCount(stopPopularizeStatistic.getOrderCount() + statistic.getOrderCount());
				stopPopularizeStatistic.setPaidOrderCount(stopPopularizeStatistic.getPaidOrderCount() + statistic.getPaidOrderCount());
				stopPopularizeStatistic.setPaymentAmount(stopPopularizeStatistic.getPaymentAmount().add(statistic.getPaymentAmount()));
			}else{
				notStopPopularizeStatistics.add(statistic);
			}
		}
		
		if(!isPopularizeStatisticAllZero(stopPopularizeStatistic)){
			notStopPopularizeStatistics.add(stopPopularizeStatistic);
		}
		
		return notStopPopularizeStatistics;
	}

	/**
	 * 是否推广统计数据全为零
	 * @param popularizeStatistic
	 * @return true - 是，false - 否
	 */
	private boolean isPopularizeStatisticAllZero(
			PopularizeStatistic popularizeStatistic) {
		
		if(popularizeStatistic.getVisitCount() > 0 ||
		   popularizeStatistic.getRegisterCount() > 0 ||
		   popularizeStatistic.getRealNameCount() > 0 ||
		   popularizeStatistic.getPaidUserCount() > 0 ||
		   popularizeStatistic.getOrderCount() > 0 ||
		   popularizeStatistic.getPaidOrderCount() > 0 ||
		   popularizeStatistic.getPaymentAmount().compareTo(BigDecimal.ZERO) == 1){
			return false;
		}
		
		return true;
	}

	/**
	 * 根据类别ID获取推广
	 * @param categoryId	类别ID
	 * @return
	 */
	private List<Popularize> getPopularizesByByCategoryId(Long categoryId) {
		
		PopularizeExample example = new PopularizeExample();
		example.appendCriterion("category_id =", categoryId);
		example.setOrderByClause("createDate desc");
		Pager<Popularize> pager = new Pager<Popularize>();
		pager.setPageSize(10000);
		return this.popularizeMapper.selectByExample(example, pager);
	}

	
	/**
	 * 统计类别ID获取统计列表
	 * @param biggerCategoryId
	 * @param categoryId
	 * @return
	 */
	@Override
	public List<PopularizeStatistic> getPopularizeStatisticByCategoryId(
			Long biggerCategoryId, 
			Long categoryId) {
		
		List<Popularize> popularizes = getPopularizesByByCategoryId(categoryId);
		
		List<PopularizeStatistic> statistics = new ArrayList<>();
		for(Popularize popularize : popularizes){
			PopularizeStatistic statistic = new PopularizeStatistic();
			statistic.setBiggerCategoryId(biggerCategoryId);
			statistic.setCategoryId(categoryId);
			statistic.setPopularizeId(popularize.getId());
			statistic.setPopularizeName(popularize.getName());
			statistic.setIsStopPopularize(popularize.getIsStopPopularize());
			statistics.add(statistic);
		}
		return statistics;
	}

	/**
	 * 统计指定类别和时间的下单支付金额
	 * @param biggerCategoryId	大类ID
	 * @param categoryId	类别ID
	 * @param startDate		开始日期
	 * @param endDate		结束日期
	 * @param statistics	推广统计列表
	 */
	private void countPaymentAmountByCategoryIdAndDate(Long biggerCategoryId,
			Long categoryId, Date startDate, Date endDate,
			List<PopularizeStatistic> statistics) {
		
		List<PopularizeCount> counts = this.popularizeVisitMapper
				.countPaymentAmountByCategoryIdAndDate(categoryId, startDate, endDate);
		for(PopularizeCount count : counts){
			for(PopularizeStatistic statistic : statistics){
				if(statistic.getPopularizeId().equals(count.getPopularizeId())){
					statistic.setPaymentAmount(count.getAmount());
				}
			}
		}
	}
	
	/**
	 * 统计指定类别和时间的支付订单数
	 * @param biggerCategoryId	大类ID
	 * @param categoryId	类别ID
	 * @param startDate		开始日期
	 * @param endDate		结束日期
	 * @param statistics	推广统计列表
	 */
	private void countPaidOrderByCategoryIdAndDate(Long biggerCategoryId,
			Long categoryId, Date startDate, Date endDate,
			List<PopularizeStatistic> statistics) {

		List<PopularizeCount> counts = this.popularizeVisitMapper
				.countPaidOrderByCategoryIdAndDate(categoryId, startDate, endDate);
		for(PopularizeCount count : counts){
			for(PopularizeStatistic statistic : statistics){
				if(statistic.getPopularizeId().equals(count.getPopularizeId())){
					statistic.setPaidOrderCount(count.getCount());
				}
			}
		}
	}
	
	/**
	 * 统计指定类别和时间的下单数
	 * @param biggerCategoryId	大类ID
	 * @param categoryId	类别ID
	 * @param startDate		开始日期
	 * @param endDate		结束日期
	 * @param statistics	推广统计列表
	 */
	private void countOrderByCategoryIdAndDate(Long biggerCategoryId,
			Long categoryId, Date startDate, Date endDate,
			List<PopularizeStatistic> statistics) {

		List<PopularizeCount> counts = this.popularizeVisitMapper
				.countOrderByCategoryIdAndDate(categoryId, startDate, endDate);
		for(PopularizeCount count : counts){
			for(PopularizeStatistic statistic : statistics){
				if(statistic.getPopularizeId().equals(count.getPopularizeId())){
					statistic.setOrderCount(count.getCount());
				}
			}
		}
	}
	
	/**
	 * 统计指定类别和时间的下单支付用户数
	 * @param biggerCategoryId	大类ID
	 * @param categoryId	类别ID
	 * @param startDate		开始日期
	 * @param endDate		结束日期
	 * @param statistics	推广统计列表
	 */
	private void countPaidUserByCategoryIdAndDate(Long biggerCategoryId,
			Long categoryId, Date startDate, Date endDate,
			List<PopularizeStatistic> statistics) {

		List<PopularizeCount> counts = this.popularizeVisitMapper
				.countPaidUserByCategoryIdAndDate(categoryId, startDate, endDate);
		
		for(PopularizeCount count : counts){
			for(PopularizeStatistic statistic : statistics){
				if(statistic.getPopularizeId().equals(count.getPopularizeId())){
					statistic.setPaidUserCount(count.getCount());
				}
			}
		}
	}
	
	/**
	 * 统计指定类别和时间的实名认证数
	 * @param biggerCategoryId	大类ID
	 * @param categoryId	类别ID
	 * @param startDate		开始日期
	 * @param endDate		结束日期
	 * @param statistics	推广统计列表
	 */
	private void countRealNameUserByCategoryIdAndDate(Long biggerCategoryId,
			Long categoryId, Date startDate, Date endDate,
			List<PopularizeStatistic> statistics) {

		List<PopularizeCount> counts = this.popularizeVisitMapper
				.countRealNameUserByCategoryIdAndDate(categoryId, startDate, endDate);
		
		for(PopularizeCount count : counts){
			for(PopularizeStatistic statistic : statistics){
				if(statistic.getPopularizeId().equals(count.getPopularizeId())){
					statistic.setRealNameCount(count.getCount());
				}
			}
		}
	}
	
	/**
	 * 统计指定类别和时间的注册数
	 * @param biggerCategoryId	大类ID
	 * @param categoryId	类别ID
	 * @param startDate		开始日期
	 * @param endDate		结束日期
	 * @param statistics	推广统计列表
	 */
	private void countRegisterByCategoryIdAndDate(Long biggerCategoryId,
			Long categoryId, Date startDate, Date endDate,
			List<PopularizeStatistic> statistics) {

		List<PopularizeCount> counts = this.popularizeVisitMapper
				.countUserRegisterByCategoryIdAndDate(categoryId, startDate, endDate);
		for(PopularizeCount count : counts){
			for(PopularizeStatistic statistic : statistics){
				if(statistic.getPopularizeId().equals(count.getPopularizeId())){
					statistic.setRegisterCount(count.getCount());
				}
			}
		}
	}
}
